package com.alex.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.alex.entity.Reply;
import com.alex.entity.Tip;
import com.alex.entity.User;
import com.alex.service.IReplyService;
import com.alex.service.ITipService;
import com.alex.service.IUserService;

public class ReplyAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private int topicId;
	private String boardId;
	private String title;
	private String content;

	private String replyid;
	private List allReplys;
	private Tip tip2;
	private String pageNostr;
	private IReplyService replyService;
	private IUserService userService;
	private ITipService tipService;
	
	public IReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(IReplyService replyService) {
		this.replyService = replyService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public ITipService getTipService() {
		return tipService;
	}

	public void setTipService(ITipService tipService) {
		this.tipService = tipService;
	}

	public String getPageNostr() {
		return pageNostr;
	}

	public void setPageNostr(String pageNostr) {
		this.pageNostr = pageNostr;
	}

	public Tip getTip2() {
		return tip2;
	}

	public void setTip2(Tip tip2) {
		this.tip2 = tip2;
	}

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}

	public List getAllReplys() {
		return allReplys;
	}

	public void setAllReplys(List allReplys) {
		this.allReplys = allReplys;
	}

	public String getReplyid() {
		return replyid;
	}

	public void setReplyid(String replyid) {
		this.replyid = replyid;
	}


	public String getBoardId() {
		return boardId;
	}

	public void setBoardId(String boardId) {
		this.boardId = boardId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * 添加回复
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addReply() throws Exception {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		ActionContext contents = ActionContext.getContext();
		Map session = contents.getSession();
		User user = (User) session.get("user");
		Integer uid = user.getUserid();
		Tip tip = tipService.findById(topicId);
		Reply reply = new Reply();
		reply.setTitle(title);
		reply.setContent(content);
		reply.setUser(user);
		reply.setPublishtime(sdf.format(date));
		reply.setTip(tip);
		
		replyService.save(reply);
		
		int pageNo = 1;
		int pageSize = 5;
		int count = replyService.findCountReply(topicId);
		int pageCount = (count + pageSize - 1) / pageSize;
		if (pageNostr == null || pageNostr.trim().equals("")) {
			pageNo = 1;
		}
		try {
			pageNo = Integer.parseInt(pageNostr.trim());
		} catch (Exception e) {
			pageNo = 1;
		}
		if (pageNo > pageCount) {
			pageNo = pageCount;
		}
		ActionContext contents2 = ActionContext.getContext();
		Map request = (Map) contents2.get("request");
		request.put("count", count);
		request.put("pageCount", pageCount);
		request.put("pageNo", pageNo);
		allReplys = replyService.findListReply(topicId, pageNo, pageSize);
		tip2 = tipService.findById(topicId);
		return SUCCESS;
	}

	/**
	 * 查找回复id
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findReplyByTipid() throws Exception {
		
		int pageNo = 1;
		int pageSize = 5;
		int count = replyService.findCountReply(topicId);
		int pageCount = (count + pageSize - 1) / pageSize;
		if (pageNostr == null || pageNostr.trim().equals("")) {
			pageNo = 1;
		}
		try {
			pageNo = Integer.parseInt(pageNostr.trim());
		} catch (Exception e) {
			pageNo = 1;
		}
		if (pageNo > pageCount) {
			pageNo = pageCount;
		}
		ActionContext contents = ActionContext.getContext();
		Map request = (Map) contents.get("request");
		request.put("count", count);
		request.put("pageCount", pageCount);
		request.put("pageNo", pageNo);
		allReplys = replyService.findListReply(topicId, pageNo, pageSize);
		tip2 = tipService.findById(topicId);
		return SUCCESS;
	}
}
